import { Component, OnInit, ViewChild, OnDestroy } from '@angular/core';
import { NzMessageService, UploadFile, NzModalService, NzNotificationService } from 'ng-zorro-antd';
import { catchError } from 'rxjs/operators';
import { of } from 'rxjs';
import { ActivatedRoute, Router } from '@angular/router';
import { TitleService } from '@delon/theme';
import { EmitterService } from 'app/service/emit.service';
import { CommService } from '../../../../service/comm/comm.service';
import { ServiceResult, Constant } from '@core/constant';
import { ProductSkuEditV2Component } from '@shared/component/product-sku-edit-v2/product-sku-edit-v2.component';
import {sysqmService} from "../../../../service/sysqm/sysqm.service";
@Component({
  selector: 'sysqm-add103',
  templateUrl: './sysqm-add103.component.html',
  styleUrls: ['./sysqm-add103.component.less']
})
export class sysqmAdd103Component implements OnInit, OnDestroy {
  product: {[propName: string]: any;} = {}
  // 预览图片对象
  previewImage = '';
  q: any = {

    vbeln: '',
    xianghao:''
  };
  // 是否可以预览
  previewVisible = false;
  pjStatus=0;
  sap104=0;
  sapqm=0;
  sap105=0;

  qmRemark4='';
  menge3='';
  downurl='';

  brands = [];
  @ViewChild("skuEdit2") skuEdit2: ProductSkuEditV2Component;
  private sub: any;
  loading = false;
  constructor(
    private msg: NzMessageService,
    private modalService: NzModalService,
    private sysqmService: sysqmService,
    private emit: EmitterService,
    private notification: NzNotificationService,
    private route: ActivatedRoute,
    private titleService: TitleService,
    private router: Router,
    private cnt: Constant,
    private comm: CommService,
    private confirmServ: NzModalService,

  ) {}
  ngOnInit(): void {
       this.int();

    setTimeout(() => {
        this.emit.sendMessage("HIDE_REUSE_TAB");
    }, 100);

  }
  ngOnDestroy() {
    this.emit.sendMessage("SHOW_REUSE_TAB");
  }
  int(){

    this.sap104=0;

    this.skuEdit2.dataTable=[];
    this.skuEdit2.dataTablesum=[];
    this.skuEdit2.dataTablebak=[];
    let xianghao = this.q.xianghao.split('/')[0];
    console.log("xianghao="+xianghao);
    if(xianghao){
      this.loading = true;

      this.sysqmService.getlist(this.q.vbeln,xianghao,'103',this.q.matnr).pipe(
        catchError(err => {
          return of(err);
        })
      ).subscribe(data => {
        this.loading = false;
        if (data.ok === true) {
          //
          let syspjPage = [];
          let syspjPagesum = [];
          // 组织数据

          if (data.data.length > 0) {
            for (const detail of data.data) {

              let syspjPageobj = [];
              let syspjPagesumobj = [];

              syspjPagesumobj[0]=detail.matnr;
              syspjPagesumobj[1]=detail.menge;

              syspjPageobj[0]=detail.mblnr;
              syspjPageobj[1]=detail.xianghao;
              syspjPageobj[2]=detail.ebenl;
              syspjPageobj[3]=detail.ebelp;
              syspjPageobj[4]=detail.matnr;
              syspjPageobj[5]=detail.lifmatnr;
              syspjPageobj[6]=detail.lgort;
              syspjPageobj[7]=detail.menge;

              syspjPageobj[8]=detail.menge1;
              syspjPageobj[9]=detail.menge2;
              syspjPageobj[10]=detail.menge3;
              syspjPageobj[11]=detail.remark;
              syspjPageobj[12] =detail.qmMenge1;
              syspjPageobj[13] =detail.qmMenge2;
              syspjPageobj[14] =detail.qmRemark1;
              syspjPageobj[15] =detail.qmRemark2;
              syspjPageobj[16] =detail.qmPanding1;
              syspjPageobj[17] =detail.qmRemark4;

              syspjPageobj[18] =detail.qmRemark5;

              syspjPageobj[19] =detail.qmRemark6;
              // syspjPageobj[20] =detail.qmRemark6;
              syspjPageobj[22]=detail.id;
              if(this.pjStatus===10){
                this.pjStatus===10;
              }else{
                this.pjStatus = detail.bpmStatus;
              }
                this.qmRemark4 = detail.qmRemark4;


                this.menge3 = detail.menge3;

              if(this.menge3&& this.qmRemark4==''){
                this.sap104=1;
              }
              console.log("this.pjStatus="+this.pjStatus);
              syspjPagesum.push(syspjPagesumobj);

              syspjPage.push(syspjPageobj);
            }
          }



          this.skuEdit2.dataTable=syspjPage;
          this.skuEdit2.dataTablesum=syspjPagesum;
          this.skuEdit2.dataTablebak=syspjPage;
        }
      })
    }else{
      if(this.q.vbeln == '' || this.q.vbeln == null){
        this.msg.warning("快递单");
        return;
      }
      this.loading = true;

      this.sysqmService.getlistvbeln(this.q.vbeln,xianghao,'103',this.q.matnr,"").pipe(
        catchError(err => {
          return of(err);
        })
      ).subscribe(data => {
        this.loading = false;
        if (data.ok === true) {
          //
          let syspjPage = [];
          // 组织数据

          var map = new Map<String,string>();

          if (data.data.length > 0) {
            for (const detail of data.data) {
              let syspjPageobj = [];

              if(map.has(detail.matnr)){
                let sum = map.get(detail.matnr);
                let sumbenci = Number(sum)+Number(detail.menge);
                map.set(detail.matnr, sumbenci.toString());

              }else{
                map.set(detail.matnr,detail.menge);
              }
              syspjPageobj[0]=detail.mblnr;
              syspjPageobj[1]=detail.xianghao;
              syspjPageobj[2]=detail.ebenl;
              syspjPageobj[3]=detail.ebelp;
              syspjPageobj[4]=detail.matnr;
              syspjPageobj[5]=detail.lifmatnr;
              syspjPageobj[6]=detail.lgort;
              syspjPageobj[7]=detail.menge;

              syspjPageobj[8]=detail.menge1;
              syspjPageobj[9]=detail.menge2;
              syspjPageobj[10]=detail.menge3;
              syspjPageobj[11]=detail.remark;
              syspjPageobj[12] =detail.qmMenge1;
              syspjPageobj[13] =detail.qmMenge2;
              syspjPageobj[14] =detail.qmRemark1;
              syspjPageobj[15] =detail.qmRemark2;
              syspjPageobj[16] =detail.qmPanding1;
              syspjPageobj[17] =detail.qmRemark4;

              syspjPageobj[18] =detail.qmRemark5;

              syspjPageobj[19] =detail.qmRemark6;

              syspjPageobj[22]=detail.id;
              if(this.pjStatus===10){
                this.pjStatus===10;
              }else{
                this.pjStatus = detail.bpmStatus;
              }
              this.qmRemark4 = detail.qmRemark4;


                this.menge3 = detail.menge3;
              console.log("this.menge3="+this.menge3);
              console.log("this.qmRemark4="+this.qmRemark4);
              if(this.menge3&& (this.qmRemark4==''||this.qmRemark4==null)){
                this.sap104=1;
              }
              if(detail.menge2&& detail.mblnr&& (detail.qmRemark6==''||detail.qmRemark6==null)){
                this.sap105=1;
              }
              if(detail.mblnr&& ((detail.qmRemark6==''||detail.qmRemark6==null)||(detail.qmRemark4==''||detail.qmRemark4==null))){
                this.sapqm=1;
              }
              console.log("this.sap104="+this.sap104);

              syspjPage.push(syspjPageobj);
            }
          }
          let syspjPagesum = [];
          map.forEach(function (value,key,map) {
            let syspjPagesumobj = [];

            syspjPagesumobj[0]=key;
            syspjPagesumobj[1]=value;
            syspjPagesum.push(syspjPagesumobj);
          })

          this.skuEdit2.dataTablesum=syspjPagesum;
          this.skuEdit2.dataTable=syspjPage;
          this.skuEdit2.dataTablebak=syspjPage;
        }
      })
    }

  }


  pacafirst(){
    if(this.q.vbeln == '' || this.q.vbeln == null){
      this.msg.warning("快递单");
      return;
    }
    this.confirmServ.confirm({
      nzTitle: '确定导出发货单?',
      nzOnOk: () => {
        this.packatta();
      },
    });
  }


  paca104(){
    if(this.q.vbeln == '' || this.q.vbeln == null){
      this.msg.warning("快递单");
      return;
    }
    this.confirmServ.confirm({
      nzTitle: '确定打印退货单?',
      nzOnOk: () => {
        this.packatta104();
      },
    });
  }

  paca105(){
    if(this.q.vbeln == '' || this.q.vbeln == null){
      this.msg.warning("快递单");
      return;
    }
    this.confirmServ.confirm({
      nzTitle: '确定打印入库单?',
      nzOnOk: () => {
        this.packatta105();
      },
    });
  }


  pacbfirst(){
    if(this.q.vbeln == '' || this.q.vbeln == null){
      this.msg.warning("快递单");
      return;
    }
    this.confirmServ.confirm({
      nzTitle: '确定打印物料标签?',
      nzOnOk: () => {
        this.packattb('');
      },
    });
  }

  packatta(){
    this.loading = true;
    let conmapList = [];

    let  conhead={
      contempPath:"https://zhaodui.oss-cn-shenzhen.aliyuncs.com/doc/htmb/hxhtmb.docx",
      conmapList:JSON.parse(JSON.stringify(conmapList)),
    }
    this.comm.genfh(this.q.vbeln,conhead).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      this.msg.success('生成成功');
      if(data.ok){
        this.loading = false;

        console.log(JSON.stringify("data"+JSON.stringify(data)));
        this.downurl = data.data.contoUrl;
        if(this.downurl!=null||this.downurl==null){
          this.opennewwindow(this.downurl);
        }
      }
    })
  }

  packatta105(){
    this.loading = true;
    let conmapList = [];

    let  conhead={
      contempPath:"https://zhaodui.oss-cn-shenzhen.aliyuncs.com/doc/htmb/hxhtmb.docx",
      conmapList:JSON.parse(JSON.stringify(conmapList)),
    }
    this.comm.genfh105(this.q.vbeln,conhead).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      this.msg.success('生成成功');
      if(data.ok){
        this.loading = false;

        console.log(JSON.stringify("data"+JSON.stringify(data)));
        this.downurl = data.data.contoUrl;
        if(this.downurl!=null||this.downurl==null){
          this.opennewwindow(this.downurl);
        }
      }
    })
  }

  packatta104(){
    this.loading = true;
    let conmapList = [];

    let  conhead={
      contempPath:"https://zhaodui.oss-cn-shenzhen.aliyuncs.com/doc/htmb/hxhtmb.docx",
      conmapList:JSON.parse(JSON.stringify(conmapList)),
    }
    this.comm.genfh104(this.q.vbeln,conhead).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      this.msg.success('生成成功');
      if(data.ok){
        this.loading = false;

        console.log(JSON.stringify("data"+JSON.stringify(data)));
        this.downurl = data.data.contoUrl;
        if(this.downurl!=null||this.downurl==null){
          this.opennewwindow(this.downurl);
        }
      }
    })
  }

  packattb(id){
    console.log("id==="+id);
    let conmapList = [];


    let  conhead={
      orderNo:id,
      conmapList:JSON.parse(JSON.stringify(conmapList))
    }
    this.comm.genbq(this.q.vbeln,conhead).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      this.msg.success('生成成功');
      if(data.ok){
        console.log(JSON.stringify("data"+JSON.stringify(data)));
        this.downurl = data.data.contoUrl;
        if(this.downurl!=null||this.downurl==null){
          this.opennewwindow(this.downurl);
        }
      }
    })
  }

  printbq(){

    let xianghao = this.q.xianghao.split('/')[0];
    if(xianghao){
      this.packattbone(xianghao);
    }else {
      this.msg.warning("箱号");
      return;
    }
  }
  query(){
    this.int();
  }
  save(saptype) {

    let data = this.skuEdit2.dataTable;
    if (data.length == 0) {
      this.msg.warning("请填写信息");
      return;
    }
    let flag="";
    let syspjPagelist = [];
    // 组织数据
    let index1=0;
    for (let prod of data) {
      index1 = index1 + 1;
             let dto = {
              id:  this.skuEdit2.dataTablebak[index1-1][22],
               mblnr: prod[0],
               xianghao: prod[1],
               ebenl: prod[2],
               ebelp: prod[3],
               matnr: prod[4],
               lifmatnr: prod[5],
               lgort: prod[6],

               menge: prod[7],
               menge1: prod[8],
               menge2: prod[9],
               menge3: prod[10],
               remark: prod[11],
               qmMenge1: prod[12],
               qmMenge2: prod[13],
               qmRemark1: prod[14],
               qmRemark2: prod[15],
               qmPanding1: prod[16],

               qmRemark4: prod[17],

               qmRemark5: prod[18],

               qmRemark6: prod[19],

            }
      syspjPagelist.push(dto);
    }

  if (syspjPagelist.length == 0) {
      this.msg.warning("请填写信息");
      return;
    }
    let syspjPage = {
      ekpoFhList:JSON.parse(JSON.stringify(syspjPagelist))
    };
    // 调用接口
    this.loading = true;
    this.sysqmService.batcheditPro(saptype,this.q.vbeln,syspjPage).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      this.loading = false;
      console.log("data="+JSON.stringify(data));
      if (data.ok === true) {
        this.msg.success(data.data);
        this.skuEdit2.dataTable=[];
        this.skuEdit2.dataTablebak=[];

        this.int();
        this.q.xianghao="";
        this.q.vbeln="";

        }else{
          this.msg.error("保存失败,错误原因为："+data.msg);
        }

    })
  }

  packattbone(id){
    console.log("id==="+id);
    let conmapList = [];


    let  conhead={
      orderNo:id,
      conmapList:JSON.parse(JSON.stringify(conmapList))
    }
    this.comm.genbq(id,conhead).pipe(
      catchError(err => {
        return of(err);
      })
    ).subscribe(data => {
      this.msg.success('生成成功');
      if(data.ok){
        console.log(JSON.stringify("data"+JSON.stringify(data)));
        this.downurl = data.data.contoUrl;
        if(this.downurl!=null||this.downurl==null){
          this.opennewwindow(this.downurl);
        }
      }
    })
  }
  opennewwindow(url){

    const newWindow = window.open(url, '_blank');
    newWindow.document.title = '下载';

  }
}
